package com.mayuanfei.workflow;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import com.aizuda.snailjob.common.log.SnailJobLog;
import com.mayuanfei.dto.BillDto;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 汇总订单任务
 *
 * @author 老马
 * @date 2025-03-25 16:53
 */
@Component
@JobExecutor(name = "summaryBillTask")
public class SummaryBillTask {
    public ExecuteResult jobExecute(JobArgs jobArgs) throws InterruptedException {
        // 获得微信账单
        BigDecimal wechatAmount = BigDecimal.valueOf(0);
        String wechat = (String) jobArgs.getWfContext("wechat");
        if(StrUtil.isNotBlank(wechat)) {
            BillDto wechatBillDto = JSONUtil.toBean(wechat, BillDto.class);
            wechatAmount = wechatBillDto.getBillAmount();
        }
        // 获得支付宝账单
        BigDecimal alipayAmount = BigDecimal.valueOf(0);
        String alipay = (String) jobArgs.getWfContext("alipay");
        if(StrUtil.isNotBlank(alipay)) {
            BillDto alipayBillDto = JSONUtil.toBean(alipay, BillDto.class);
            alipayAmount = alipayBillDto.getBillAmount();
        }

        // 汇总账单
        BigDecimal totalAmount = wechatAmount.add(alipayAmount);
        SnailJobLog.REMOTE.info("总金额: {}", totalAmount);


        return ExecuteResult.success(totalAmount);
    }
}